﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace h2100
{
    public class hMap:hVisibleGameEntity
    {
        private int _nColumns;
        public int NColumns
        {
            get { return _nColumns; }
            set { _nColumns = value; }
        }

        private int _nRows;
        public int NRows
        {
            get { return _nRows; }
            set { _nRows = value; }
        }

        public int NCells
        {
            get { return _nRows*_nColumns; }
        }
            
        private hMapCell[,] _mapCells;
        public hMapCell[,] MapCells
        {
            get { return _mapCells; }
        }

        public override hVisibleGameEntity clone()
        {
            return null;
        }        

        public hMap(ContentManager content, int nRows, int nColumns, String[] strMap, string[] argType)
        {
            X=0;
            Y=0;

            _nColumns = nColumns;
            _nRows = nRows;

            _mapCells = new hMapCell[_nRows, _nColumns];

            for (int i = 0; i < _nRows; i++)
            {
                for (int j = 0; j < _nColumns; j++)
                {
                    Texture2D[] texture = new Texture2D[1];
                    texture[0] = content.Load<Texture2D>(hGlobal.PathTerrain+strMap[i * _nColumns + j]);
                    _mapCells[i, j] = new hMapCell(content,texture);
                    _mapCells[i, j].Type = argType[i * _nColumns + j];
                }
            }

            float delta = 0;

            for (int i = 0; i < _nRows; i++)
            {
                for (int j = 0; j < _nColumns; j++)
                {
                    if (_mapCells[i,j].Height > hGlobal.MapCellHeight)
                    {
                        delta = _mapCells[i,j].Height - hGlobal.MapCellHeight;
                    }
                    else 
                    {
                        delta = 0;
                    }
                    _mapCells[i,j].setPosition(j * hGlobal.MapCellWidth, i * hGlobal.MapCellHeight - delta);                    
                }
            }

            Width = _nColumns * hGlobal.MapCellWidth;
            Height = _nRows * hGlobal.MapCellHeight;
        }

        public void clearFog(Vector2 point)
        {
            for (int i = 0; i < NRows; i++)
                for (int j = 0; j < NColumns; j++)
                {
                    MapCells[i, j].clearFog(point);
                }
        }

        public override void update(hParams args, GameTime gameTime)
        {
            X = args.Grid.X;
            Y = args.Grid.Y;

            for (int i = 0; i < _nRows; i++)
            {
                for (int j = 0; j < _nColumns; j++)
                {
                    _mapCells[i, j].setPosition(args.Grid.Grid[i, j].X, args.Grid.Grid[i, j].Y);
                    _mapCells[i, j].update(args, gameTime);

                    if (_mapCells[i, j].Selected)
                    {
                        if (_mapCells[i, j].onFogOfWar)
                        {
                            _mapCells[i, j].Selected = false;
                        }
                        else
                        {
                            //args.SelectedMapCellPosition = new Vector2(_mapCells[i, j].X, _mapCells[i, j].Y);
                            args.SelectedMapCell = _mapCells[i, j];
                            args.SelectedMapCellPosition = new Vector2(args.SelectedMapCell.X, args.SelectedMapCell.Y);
                            args.SelectedMapCellIndexRowColumn = new Point(i, j);                            
                        }
                    }
                    
                }
            }
        }

        public new void draw(GameTime gtime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _nRows; i++)
            {
                for (int j = 0; j < _nColumns; j++)
                {                    
                    _mapCells[i,j].draw(gtime, spriteBatch);                                                      
                }
            }
        }
    }
}
